package spring.proxy;

import java.util.HashMap;
import java.util.Map;

public class ProxyFactoryCreator {

    private static final Map<ProxyType, ProxyFactory> CACHE_MAP = new HashMap<>();

    private ProxyFactoryCreator() {
    }

    public static ProxyFactoryCreator getInstance() {
        return Bean.INSTANCE;
    }

    public ProxyFactory createFactory(ProxyType proxyType) {
        if (CACHE_MAP.containsKey(proxyType)) {
            return CACHE_MAP.get(proxyType);
        }
        ProxyFactory proxyFactory;
        if (proxyType == ProxyType.CGLIB) {
            proxyFactory = new CglibProxyFactory();
        } else {
            proxyFactory = new JdkProxyFactory();
        }
        CACHE_MAP.put(proxyType, proxyFactory);
        return proxyFactory;
    }

    private static class Bean {
        private static final ProxyFactoryCreator INSTANCE = new ProxyFactoryCreator();
    }
}
